﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TravelWeb.Models;

namespace TravelWeb.Controllers
{ 
    public class PromotionController : Controller
    {
        private TravelWebDB db = new TravelWebDB();

        //
        // GET: /Promotion/

        public ViewResult Index()
        {
            return View(db.Promotions.ToList());
        }

        //
        // GET: /Promotion/Details/5

        public ViewResult Details(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // GET: /Promotion/Create

        public ActionResult Create()
        {
            Promotion promotion = new Promotion();
            promotion.Status = "New";
            return View(promotion);
        } 

        //
        // POST: /Promotion/Create

        [HttpPost]
        public ActionResult Create(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Promotions.Add(promotion);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(promotion);
        }
        
        //
        // GET: /Promotion/Edit/5
 
        public ActionResult Edit(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // POST: /Promotion/Edit/5

        [HttpPost]
        public ActionResult Edit(Promotion promotion)
        {
            if (ModelState.IsValid)
            {
                db.Entry(promotion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(promotion);
        }

        //
        // GET: /Promotion/Delete/5
 
        public ActionResult Delete(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            return View(promotion);
        }

        //
        // POST: /Promotion/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Promotion promotion = db.Promotions.Find(id);
            //db.Promotions.Remove(promotion);
            promotion.Status = "Canceled";
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Approve(int id)
        {
            Promotion promotion = db.Promotions.Find(id);
            promotion.Status = "Approved";
            db.SaveChanges();
            return RedirectToAction("Index","Promotion");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}